cc.Class({
    extends: cc.Component,

    properties: {
        rope_empty: {
            type: cc.SpriteFrame,
            default: null
        },
        rope_cow: {
            type: cc.SpriteFrame,
            default: []
        }
    },
    onLoad () {
        this.is_throw = false
        this.game = cc.find('Home').getComponent('game')
        this.fireworks = this.node.getChildByName('fireworks').getComponent(cc.ParticleSystem)
    },
    throwRope () {
        if (this.is_throw) return
        this.is_throw = true
        var top = cc.moveTo(0.5, cc.v2(0, 0))  // (duration, v2)
        var topFn = cc.callFunc(function() {  // 抛出到顶点时的回调函数
            var index = this.game.catchCow()
            if (index !== -1) { // 套到了牛
                this.fireworks.resetSystem()
                // 换为牛&绳
                this.setRope(index)
            }
        }.bind(this), this.node)
        var finish = cc.moveTo(0.5, cc.v2(0, -500))  // 收回后的回调函数
        var finishFn = cc.callFunc(function() {
            this.is_throw = false
            // 换为空绳
            this.emptyRope()
        }.bind(this), this.node)
        var seq = cc.sequence([top, topFn, finish, finishFn])
        this.node.runAction(seq)
    },
    setRope(index) {
        var rope_sprite = this.node.getComponent(cc.Sprite)
        rope_sprite.spriteFrame = this.rope_cow[index]
    },
    emptyRope() {
        var rope_empty_sprite = this.node.getComponent(cc.Sprite)
        rope_empty_sprite.spriteFrame = this.rope_empty
    },
    start () {

    }
});
